/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useRef } from 'react';
import type { ActionType, ProColumns } from '@ant-design/pro-table';
import ProTable from '@ant-design/pro-table';
import DragTable from '@/components/DragTable';
import { Button, Modal, Space, message } from 'antd';

interface KbDbTransferModalProps {
  isOpened: boolean;
  requestList: any;
  updateIsOpened: (isOpened: boolean) => void;
}
interface dbTransferExprotItem {
  id: string;
  created_at: string;
  file_name: string;
  file_path: string;
  status: string;
  user: string;
}

const KbDbTransferModal: React.FC<KbDbTransferModalProps> = (props) => {
  const { isOpened, updateIsOpened, requestList } = props;
  const actionRef = useRef<ActionType>();

  const columns: ProColumns<dbTransferExprotItem>[] = [
    {
      title: '对账单名称',
      dataIndex: 'file_path',
      align: 'center',
      width: '200px',
      ellipsis: true,
    },
    {
      title: '操作人',
      dataIndex: 'adm_name',
      align: 'center',
      width: '120px',
      ellipsis: true,
    },
    {
      title: '操作时间',
      dataIndex: 'created_at',
      align: 'center',
      width: '160px',
      ellipsis: true,
    },
    {
      title: '文件状态',
      dataIndex: 'status_desc',
      align: 'center',
      width: '120px',
      ellipsis: true,
    },
    {
      title: '操作',
      align: 'center',
      width: '120px',
      fixed: 'right',
      ellipsis: true,
      render: function (_: any, d: any) {
        return d.status == 2 ? (
          <Button size='small' type='primary' onClick={() => window.open(d.file_url)}>
            下载
          </Button>
        ) : (
          '--'
        );
      },
    },
  ];

  const handleClose = () => {
    updateIsOpened(false);
  };

  const getExcelList = (values: any) => {
    return requestList({
      page: values.current,
      page_size: values.pageSize,
    }).then((res: any) => {
      const { code, data, msg } = res;
      if (code == 0) {
        return {
          data: data.data || [],
          total: data.total_num || 0,
        };
      } else {
        message.error(msg);
        return {
          data: [],
        };
      }
    });
  };

  return (
    <Modal
      width={1200}
      open={isOpened}
      destroyOnClose={true}
      onCancel={handleClose}
      onOk={handleClose}
    >
      <DragTable>
        <ProTable
          cardProps={{
            title: <Space>下载的文件最多保存15天，请尽快下载</Space>,
          }}
          rowKey='file_path'
          actionRef={actionRef}
          request={getExcelList}
          columns={columns}
          pagination={false}
          search={false}
          scroll={{
            x: 1000,
          }}
          options={false}
        />
      </DragTable>
    </Modal>
  );
};

export default KbDbTransferModal;
